google.load("visualization", "1");

function isIE()
{
	return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}

function drawSurface(chartTarget,rows, cols, x_label, x_start, x_step, y_label, y_start, y_step, zMap, max, z_label)
{	
	
	var numRows = rows;
	var numCols = cols;

	var tooltipStrings = new Array();
	var data = new google.visualization.DataTable();

	for (var i = 0; i < numCols; i++)
	{
		data.addColumn('number', 'col'+i);
	}

	data.addRows(numRows);
	var d = 360 / numRows;
	var idx = 0;

	for (var i = 0; i < numRows; i++) 
	{
		
		for (var j = 0; j < numCols; j++)
		{
			var zCol = zMap[j].substring(1,zMap[j].length-2).split(",");
			var value = parseFloat(zCol[i]);
			if(!isFinite(value)){
				value = 0;
			}
			var x = x_start + (i*x_step);
			var y = y_start + (j*y_step);
			data.setValue(i, j, value/(2*max));

			tooltipStrings[idx] = x_label + ": " + x + "<br/>" + y_label + ": " + y + "<br/>" + z_label + ": " + value;
			idx++;
		}
	}

	var surfacePlot = new greg.ross.visualisation.SurfacePlot(document.getElementById(chartTarget));

	// Don't fill polygons in IE. It's too slow.
	var fillPly = !isIE();

	// Define a colour gradient.
	var colour1 = {red:0, green:0, blue:255};
	var colour2 = {red:0, green:255, blue:255};
	var colour3 = {red:0, green:255, blue:0};
	var colour4 = {red:255, green:255, blue:0};
	var colour5 = {red:255, green:0, blue:0};
	var colours = [colour1, colour2, colour3, colour4, colour5];

	// Axis labels.
	var xAxisHeader	= x_label;
	var yAxisHeader	= y_label;
	var zAxisHeader	= z_label;

	var options = {xPos: 0, yPos: 0, width: 640, height: 477, colourGradient: colours, fillPolygons: fillPly,
			tooltips: tooltipStrings, xTitle: xAxisHeader, yTitle: yAxisHeader, zTitle: zAxisHeader};

	surfacePlot.draw(data, options);
}